//
//  LSZVideoPlayBackWebVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/9/28.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZVideoPlayBackWebVC.h"

@interface LSZVideoPlayBackWebVC ()

@end

@implementation LSZVideoPlayBackWebVC
-(void)clickButReturn
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString stringWithFormat:@"%@",self.titleStr];
    UIView * viewTop = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, NAVIGATION_BAR_HEIGHT)];
    viewTop.backgroundColor = RGBColor(224,106,103);
    viewTop.userInteractionEnabled = YES;
    [self.view addSubview:viewTop];
    
    UILabel * labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(125, NAVIGATION_BAR_HEIGHT-35, ScreenWidth-250, 20)];
    labelTitle.textColor = [UIColor whiteColor];
    labelTitle.textAlignment = NSTextAlignmentCenter;
    [viewTop addSubview:labelTitle];
    labelTitle.font = FONT_20;
    
    
    UIButton * but = [[UIButton alloc]init];
    but.frame = CGRectMake(0, 0, 100, NAVIGATION_BAR_HEIGHT);
    [but addTarget:self action:@selector(clickButReturn) forControlEvents:UIControlEventTouchUpInside];
    [viewTop addSubview:but];
    
    UIImageView * imageViewReturn = [[UIImageView alloc]initWithFrame:CGRectMake(20, but.frame.size.height-35, 11, 20)];
    imageViewReturn.image = [UIImage imageNamed:@"home_nav_button_back"];
    [viewTop addSubview:imageViewReturn];
    
//    [_LCPlayer pause];
    WKWebViewConfiguration*config = [[WKWebViewConfiguration alloc]init];
    
    config.selectionGranularity = WKSelectionGranularityCharacter;
    
//    WKWebView  *webView= [[WKWebView alloc]initWithFrame:CGRectZero configuration:config];
    self.webViewPlay = [[WKWebView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, ScreenHeight-NAVIGATION_BAR_HEIGHT) configuration:config];
//    self.webViewPlay = [[WKWebView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, ScreenHeight-NAVIGATION_BAR_HEIGHT)];
    NSString *urlString = [NSString stringWithFormat:@"%@",self.playurl];
    self.webViewPlay.UIDelegate = self;
    self.webViewPlay.navigationDelegate = self;
//    self.webViewPlay.allowsLinkPreview = YES;

  
    [self.webViewPlay loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
    [self.view addSubview:self.webViewPlay];
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

    if ([(navigationAction.request.URL.absoluteString)hasPrefix:@"http://fuyin.app.inping.com/"]){
        NSLog(@"11111");
    }

    NSString *url = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
    NSLog(@"url = %@", url);
    //提交请求时候分割参数的分隔符
    NSArray *components = [url componentsSeparatedByString:@"#"];
    if ([components count] > 1)
    {
        self.value = 1;
        if (_LCPlayer.kstatus == LCMusicPlayerStatusPlaying ){
            //            [_PlaylistMgr removeAllPlaylists];
            [_LCPlayer pause];//暂停播放
            self.PlayAudio = 1;
            _LCPlayer.comeFrome = 1;
            decisionHandler(WKNavigationActionPolicyAllow);
            return;
        }
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.webViewPlay loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
}
-(void)viewWillDisappear:(BOOL)animated
{
    if (self.value == 1) {
        [super viewDidDisappear:animated];

        [self.webViewPlay loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
        [self.webViewPlay evaluateJavaScript:@"pauseVideo()" completionHandler:^(id object, NSError * _Nullable error) {
        }];
//        mWebView.evaluateJavaScript("pauseVideo()") { (data, error) in

//        self.
//        self.webViewPlay = nil;
//        self.webViewPlay.evaluateJavaScript("pauseVideo()") { (data, error) in
//        }
//        self.webViewPlay.evaluateJavaScript("pauseAudio()") { (data, error) in
//        }
        
//        [self.webViewPlay evaluateJavaScript:@"pauseVideo()" completionHandler:^(id object, NSError * _Nullable error) {
//
//        }];
//        [self.webViewPlay evaluateJavaScript:@"pauseAudio()" completionHandler:^(id object, NSError * _Nullable error) {
//
//        }];

        _LCPlayer.comeFrome = 0;
 
    }
    if (self.PlayAudio == 1) {
        [_LCPlayer play];//继续播放
    }
}
@end
